package com.example.skyao.medicine.classes;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by Peter on 2015/11/18.
 */
public final class AppHandler implements DatabaseHandler {
    @Override
    public void handleLoad(SQLiteDatabase db) {
        Cursor cursor = db.rawQuery("select * from appInfo", null);
        while (cursor.moveToNext()) {
            AppFactory.getInstance().setEmotionPassword(cursor.getString(2));
            AppFactory.getInstance().setEmotionActivated(Boolean.parseBoolean(cursor.getString(3)));
            AppFactory.getInstance().setStartupPassword(cursor.getString(4));
            AppFactory.getInstance().setStartupActivated(Boolean.parseBoolean(cursor.getString(5)));
            Reminder.setCOUNT(cursor.getInt(6));
            Emotion.setCOUNT(cursor.getInt(7));
        }
    }

    @Override
    public void handleSave(SQLiteDatabase db) {
        db.execSQL("delete from appInfo");
        db.execSQL("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'appInfo'");
        ContentValues contentValues = new ContentValues();
        contentValues.put("emotionPassword", AppFactory.getInstance().getEmotionPassword());
        contentValues.put("emotionActivated", AppFactory.getInstance().isEmotionActivated());
        contentValues.put("startupPassword", AppFactory.getInstance().getStartupPassword());
        contentValues.put("startupActivated", AppFactory.getInstance().isStartupActivated());
        contentValues.put("reminderCount", Reminder.getCOUNT());
        contentValues.put("emotionCount", Emotion.getCOUNT());
        db.insert("appInfo", null, contentValues);
    }
}
